package com.woniuxy.joylifemarket.service.impl;

import com.woniuxy.common.entity.CenterUser;
import com.woniuxy.common.entity.MarketCollect;
import com.woniuxy.common.entity.MarketCommodity;
import com.woniuxy.common.mapper.CenterUserMapper;
import com.woniuxy.common.mapper.MarketCollectMapper;
import com.woniuxy.common.mapper.MarketCommodityMapper;
import com.woniuxy.common.utils.KeyUtil;
import com.woniuxy.joylifemarket.service.MarketCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MarketCollectServiceImpl implements MarketCollectService {
    @Autowired
    MarketCollectMapper marketCollectMapper;
    @Autowired
    MarketCommodityMapper marketCommodityMapper;
    @Autowired
    CenterUserMapper centerUserMapper;

    @Override
    public List<MarketCollect> findAllCollectByUserId(String userid) {
        List<MarketCollect> marketCollects = marketCollectMapper.selectMyCollectByUserId(userid);
        for (MarketCollect marketCollect:marketCollects) {
            MarketCommodity marketCommodity = marketCommodityMapper.selectByPrimaryKey(marketCollect.getCommodityId());
            CenterUser user = centerUserMapper.selectByPrimaryKey(marketCommodity.getUserId());
            marketCommodity.setUser(user);
            marketCollect.setCommodity(marketCommodity);
        }
        return marketCollects;
    }

    @Override
    public Boolean findCollectStatus(String commodityid, String loginuserid) {
        MarketCollect marketCollect = marketCollectMapper.selectCollectStatus(commodityid, loginuserid);
        if (marketCollect==null) {
            return false;
        }
        return true;
    }

    @Override
    public int insertCollect(String commodityid, String loginuserid) {
        MarketCollect marketCollect = new MarketCollect();
        marketCollect.setId(KeyUtil.generateKey());
        marketCollect.setCommodityId(commodityid);
        marketCollect.setUserId(loginuserid);
        marketCollect.setDate(new Date());
        int i = marketCollectMapper.insert(marketCollect);
        return i;
    }

    @Override
    public int deleteCollet(String commodityid, String loginuserid) {
        MarketCollect marketCollect = marketCollectMapper.selectCollectStatus(commodityid, loginuserid);
        int i = marketCollectMapper.deleteByPrimaryKey(marketCollect.getId());
        return i;
    }
}
